package com.vcredit.bird.service.zmxy;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.antgroup.zmxy.openplatform.api.internal.util.RSACoderUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.bird.enumtype.ZmxyCallBackStatusEnum;
import com.vcredit.bird.vo.zmxy.ZmxyCallBackResult;
import com.vcredit.bird.vo.zmxy.ZmxyQueryResult;
import com.vcredit.bird.vo.zmxy.ZmxyScoreDetail;
import com.vcredit.creditcat.util.Constants;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.BaseException;
import com.vcredit.framework.util.Base64Util;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;

@Service
public class ZmxyProcessService {
	@Value("${zmxy.query.url}")
	private String ZMXY_QUERY_URL;
	@Value("${zmxy.detail.url}")
	private String ZMXY_DETAIL_URL;
	@Value("${zmxy.bustype}")
	private String ZMXY_BUSTYPE;
	@Log
	public ZmxyQueryResult zmxyQuery(String identity, String name, String busType, String platform) throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Identity", identity);
		map.put("Name", name);
		map.put("BusType", Constants.ZMXY_BUSTYPE);
		map.put("Platform", Constants.ZMXY_PLATFORM);
		String result = HttpRequest.doJson(ZMXY_QUERY_URL, Base64Util.base64Encode(JacksonUtil.Obj2Json(map)));
		return JacksonUtil.Json2Obj(result, new TypeReference<ZmxyQueryResult>() {
		});
	}

	@Log
	public ZmxyCallBackResult zmxyCallBack(String params, String sign) throws Exception {
		if(params.indexOf("%")!=-1){
			params = URLDecoder.decode(params, "UTF-8");
		}
		String content = RSACoderUtil.decrypt(params, Constants.ZMXY_PRIVATE_KEY, "UTF-8");	
		Map<String, String> paramMap = convertParam(content);
		if (paramMap.get("success").equals("true")) {
			return new ZmxyCallBackResult(ZmxyCallBackStatusEnum.SUCCESS.getCode());
		} else {
			String errorCode = paramMap.get("error_code");
			String errorMessage = URLDecoder.decode(paramMap.get("error_message"), "UTF-8");
			if (errorCode.startsWith("ZMCSP")) {
				return new ZmxyCallBackResult(ZmxyCallBackStatusEnum.FAIL.getCode(), errorMessage);
			}
			return new ZmxyCallBackResult(ZmxyCallBackStatusEnum.RE_DO_AVAIL.getCode(), errorMessage);
		}
	}

	private Map<String, String> convertParam(String str) {
		String[] paramPairs = str.split("&");
		Map<String, String> map = new HashMap<String, String>();
		String paramPair = null;

		for (int i = 0; i < paramPairs.length; i++) {
			paramPair = paramPairs[i];
			map.put(paramPair.split("=")[0], paramPair.split("=")[1]);
		}
		return map;
	}
	
	/**
	 * 查询行业关注名单
	 * @author zhaoqi01
	 * @return
	 * @throws Exception 
	 * @throws JsonProcessingException 
	 */
	@Log
	public List<ZmxyScoreDetail> queryZmfDetail(String idno, String name) throws JsonProcessingException, Exception {
		List<ZmxyScoreDetail> result = new ArrayList<ZmxyScoreDetail>();
		
		Map<String,String> paramMap = new HashMap<String, String>();
		paramMap.put("Identity", idno);
		paramMap.put("Name", name);
		paramMap.put("BusType", Constants.ZMXY_BUSTYPE);
		paramMap.put("Platform", Constants.ZMXY_PLATFORM);
		
		String resultStr = HttpRequest.doJson(ZMXY_DETAIL_URL, Base64Util.base64Encode(JacksonUtil.Obj2Json(paramMap)));
		ZmxyQueryResult zmxyQueryResult = JacksonUtil.Json2Obj(resultStr, new TypeReference<ZmxyQueryResult>() {});
		Integer statusCode = zmxyQueryResult.getStatusCode();
		if(statusCode == 0) { // 命中风险
			return JacksonUtil.Json2Obj(zmxyQueryResult.getResult(), new TypeReference<List<ZmxyScoreDetail>>() {});
		} else if(statusCode == 3) { // 未命中风险
			return result; 
		} else if(statusCode == 2) { // 需要授权
			throw new BaseException("芝麻信用未授权");
		} else {
			throw new BaseException("芝麻信用行业名单查询失败");
		}
	}
}
